package com.realtybaron.api;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: marc Date: Feb 3, 2010 Time: 7:30:53 AM
 * <p/>
 * THIS SOFTWARE IS COPYRIGHTED.  THE SOFTWARE MAY NOT BE COPIED REPRODUCED, TRANSLATED, OR REDUCED TO ANY ELECTRONIC MEDIUM OR MACHINE READABLE FORM WITHOUT THE PRIOR WRITTEN CONSENT OF SOCO
 * TECHNOLOGIES.
 */
public class DefaultViewResolver implements ViewResolver {
  private String prefix;
  private String suffix;
  @Inject
  public DefaultViewResolver(@Named("prefix") String prefix, @Named("suffix") String suffix) {
    this.prefix = prefix;
    this.suffix = suffix;
  }
  public void handle(HttpServletRequest req, HttpServletResponse res, String name) throws IOException, ServletException {
    if (name.startsWith("redirect:")) {
      String url = StringUtils.substringAfter(name, "redirect:");
      res.sendRedirect(res.encodeRedirectURL(url));
    } else {
      String url = this.prefix + name + this.suffix;
      req.getRequestDispatcher(url).forward(req, res);
    }
  }
}
